import java.util.Scanner;

/**
 * @author gao
 * @date 2025/7/21 16:20
 */

public class Solution {
    public int maxProfit(int[] prices, int fee) {
        int n = prices.length;
        // 分别表示第i天持有股票和第i天不持有股票的最大利润
        int[] f = new int[n];
        int[] g = new int[n];

        f[0] = -prices[0];
        // 从第1天开始遍历数组
        for (int i = 1; i < n; i++) {
            f[i] = Math.max(f[i - 1], g[i - 1] - prices[i]);
            g[i] = Math.max(g[i - 1], f[i - 1] + prices[i] - fee);
        }

        // 返回最后一天持有股票和不持有股票的最大利润的最大值
        return Math.max(f[n - 1], g[n - 1]);
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int n = in.nextInt();
            int[] prices = new int[n];
            for (int i = 0; i < prices.length; i++) {
                prices[i] = in.nextInt();
            }
            int fee = in.nextInt();
            Solution solution = new Solution();
            System.out.println(solution.maxProfit(prices, fee));
        }
    }
}